﻿

using System;
using System.Runtime.InteropServices;
using CS_Util;

namespace CS_ServerDLL {
    public class C_Console : I_Log {
        public static C_LogTxtSave o_localLog;
        byte showLogLevel;
        public C_Console S_Init(string onlyName, string prefixPath = null, byte showLogLevel=10) {
            if (string.IsNullOrEmpty(onlyName) == false) {
                o_localLog = new C_LogTxtSave(onlyName, prefixPath);
            }
            this.showLogLevel = showLogLevel;
            C_Log.o_logObj = this;
            SetConsoleCtrlHandler(d_cancelHandler, true);
            return this;
        }
        public void S_Log(byte type, string tag, string time, string log, string stackTrace) {
            if (o_localLog != null) {
                o_localLog.S_AddLog(type, tag, time, log, stackTrace);
            }
            Console.WriteLine(log);
        }
        //~C_Console() {
        //    if (localLog != null) {
        //        localLog.S_SaveData();
        //    }
        //}


        public delegate bool d_ControlCtrlDelegate(int CtrlType);
        [DllImport("kernel32.dll")]
        static extern bool SetConsoleCtrlHandler(d_ControlCtrlDelegate d_HandlerRoutine, bool Add);
        static d_ControlCtrlDelegate d_cancelHandler = new d_ControlCtrlDelegate(S_HandlerRoutine);
        public static bool S_HandlerRoutine(int CtrlType) {
            switch (CtrlType) {
                case 0:
                    Console.WriteLine("0工具被强制关闭"); //Ctrl+C关闭  
                    C_LogTxtSave.S_SaveData();
                    break;
                case 2:
                    Console.WriteLine("2工具被强制关闭");//按控制台关闭按钮关闭  
                    C_LogTxtSave.S_SaveData();
                    break;
            }
            //Console.ReadLine();
            return false;
        }

        public static void S_while() {
            while (true) {
                string ss = Console.ReadLine();
                ss = ss.Trim().ToLower();
                string[] sss = ss.Split(new string[] { " ", "_" }, StringSplitOptions.RemoveEmptyEntries);
                ss = sss.ToStringLog();
                C_Log.LogFormat("-----执行输入命令-----------".S_SetLogColor("ff0066"));
                switch (ss) {
                    case "log,_save":
                        C_LogTxtSave.S_SaveData();
                        break;
                    case "h":
                    case "help":
                        Console.WriteLine(cmdTag);
                        break;
                    default:

                        break;
                }

            }
        }
        static string cmdTag = @"
----------------------------------------------------------------
log save   保存本地log
---------------------------------------------------------------------
";
    }



}